<?php

namespace App\Repositories;

class GameSugarStatisticsRepository
{
    //日数据统计
    public function dayDataStatistics($date = '2021-06-21')
    {
        $model = app('model_game_sugar_statistics');
        $timeRange = [strtotime($date . ' 0:0:0'), strtotime($date . ' 23:59:59')];
        $data = [
            'date' => $date,
            'first_sugar_num' => $this->getDateSugarByType($timeRange, 1),
            'second_sugar_num' => $this->getDateSugarByType($timeRange, 2),
            'third_sugar_num' => $this->getDateSugarByType($timeRange, 3),
            'create_time' => time(),
            'update_time' => time()
        ];
        $record = $model->where('date', $date)->first();
        if ($record) {
            $model->where('id', $record->id)->update($data);
        } else {
            $data['date'] = $date;
            $model->insert($data);
        }
    }

    // 查询日糖果数
    public function getDateSugarByType($timeRange, $type)
    {
        $key = 'game:sugar:static:' . date("Ymd", $timeRange[0]) . ':sugar_num:type:' . $type;
        $cache = app('redis')->get($key);
        if(!empty($cache)){
            return $cache;
        }
        return app('model_game_sugar_info')
            ->whereBetween('end_time', $timeRange)
            ->where([['type', '=', $type]])
            ->sum('cost');
    }

    // 获取总数信息
    public function getTotalInfo()
    {
        $totalSugarNum = app('redis')->get('game:sugar:total:num'); // 总糖果数
        $totalStartNum = app('redis')->get('game:sugar:total:start'); // 总开奖次数
        $totalUserNum = app('redis')->pfcount('game:sugar:total:user'); // 用户参与总人数
        return [
            'sugar_num' => $totalSugarNum,
            'start_num' => $totalStartNum,
            'user_num' => $totalUserNum
        ];
    }

    // 获取今日糖果三种情况
    public function getSugarInfoByDate($date)
    {
        $ymd = date("Ymd", strtotime($date));
        // 今日3种收入情况.
        $firstSugarNumKey = "game:sugar:static:" . $ymd . ":sugar_num:type:" . 1;
        $secondSugarNumKey = "game:sugar:static:" . $ymd . ":sugar_num:type:" . 2;
        $thirdSugarNumKey = "game:sugar:static:" . $ymd . ":sugar_num:type:" . 3;
        $firstSugarNum = app('redis')->get($firstSugarNumKey);
        $secondSugarNum = app('redis')->get($secondSugarNumKey);
        $thirdSugarNum = app('redis')->get($thirdSugarNumKey);
        $firstSugarNum = empty($firstSugarNum) ? 0 : $firstSugarNum;
        $secondSugarNum = empty($secondSugarNum) ? 0 : $secondSugarNum;
        $thirdSugarNum = empty($thirdSugarNum) ? 0 : $thirdSugarNum;
        return [
            'date' => $date,
            'first_sugar_num' => $firstSugarNum,
            'second_sugar_num' => $secondSugarNum,
            'third_sugar_num' => $thirdSugarNum
        ];
    }

    // 获取历史连续n天3种糖果信息. getData: 是否包含今天
    public function getHistorySugarInfoByDate($date, $dayNum = 12, $getDate = true)
    {
        $dayNum = $getDate ? $dayNum - 1 : $dayNum;
        $time = strtotime($date);
        $historyData = app('model_game_sugar_statistics')
            ->where([['date', '<', $time]]) // 不获取今天的数据.
            ->selectRaw('date, first_sugar_num, second_sugar_num, third_sugar_num')
            ->orderBy('date', 'desc')
            ->limit($dayNum)
            ->get()->toArray();
        if ($getDate) {
            array_unshift($historyData, $this->getSugarInfoByDate($date));
        }
        foreach ($historyData as &$v) {
            $v['total_sugar_num'] = $v['first_sugar_num'] + $v['second_sugar_num'] + $v['third_sugar_num'];
            $v['show_date'] = substr($v['date'], -2, 2);
        }
        return $historyData;
    }

}

